<template>
  <div class="createMedicinePrescription-container">
    <div class="top-title-container">
      <modules-title :title-name="title">
        <template slot="btn">
          <el-button
            class="main-btn"
            size="small"
            type="primary"
            @click="submit"
            v-if="detail!='1'"
            >保存</el-button
          >
          <el-button
            class="main-btn"
            size="small"
            type="primary"
            @click="changeEdit"
            v-if="detail=='1'"
          >编辑</el-button
          >
          <el-button class="border-btn" size="small" @click="cancelOption"
            >取消</el-button
          >
        </template>
      </modules-title>
    </div>
    <div class="create-form-container">
      <medicine-prescription
        ref="createComp"
        :detail="detail"
        :edit-data="editData"
      ></medicine-prescription>
    </div>
  </div>
</template>
<script>
import medicinePrescription from "../modules/medicinePrescription";
import {
  prescriptionCreate,
  prescriptionDetails,
  prescriptionEdit,
} from "@/api/templates.js";
name: "createMedicinePrescription";
export default {
  components: {
    medicinePrescription,
  },
  data() {
    return {
      submitData: null,
      editData: null,
      editId: 0,
      types: 1, // 1,个人模板，2，诊所模板
      detail:'',
      title:''
    };
  },
  created() {
    this.types = Number(this.$route.query.types);
    this.detail = this.$route.query.detail;
    this.editId = Number(this.$route.query.id)
      ? Number(this.$route.query.id)
      : 0;
    if(this.detail=='1'){
      this.title = '详情'
    }else if(this.editId >0){
      this.title = '编辑成药处方'
    }else{
      this.title = '新建成药处方'
    }
    if (this.editId) {
      this.getPrescriptionDetails(this.editId);
    }
  },
  methods: {
    changeEdit(){
      this.detail=''
      this.title='编辑成药处方'
    },
    cancelOption() {
      if(this.types==1){
        this.$router.push({
          name: "personalPrescription",
        });
      }else{
        this.$router.push({
          name: "clinicPrescription",
        });
      }
    },
    submit() {
      this.submitData = this.$refs.createComp.emitData();
      var flag = this.submitData.prescription.details.some(item=>{
         return item.product_name!=''
      })
      if(flag){
        let boo =  this.$refs.createComp.submitForm()
        if(boo){
          if (this.editId) {
            this.submitPrescriptionEdit();
          } else {
            this.submitPrescriptionCreate();
          }
        }
      }else{
        this.$message.warning('请填写相关处方内容')
        return
      }

    },
    submitDataOption() {
      this.submitData.prescription.details.forEach((item, index) => {
        if (item.product_name == "") {
          this.submitData.prescription.details.splice(index, 1);
        } else {
          item.number = Number(item.number);
          item.single_dose = Number(item.single_dose);
          item.group = index + 1;
        }
      });
      this.submitData.prescription.group_count = this.submitData.prescription.details.length;
    },
    submitPrescriptionCreate() {
      this.submitData = this.$refs.createComp.emitData();
      this.submitDataOption();
      let submit = {
        template_name: this.submitData.prescription.prescription_name,
        pre_type: this.types,
        prescription: this.submitData.prescription,
      };
      prescriptionCreate(submit).then((res) => {
        console.log(res);
        if(this.types==1){
          this.$router.push({
            name: "personalPrescription",
          });
        }else{
          this.$router.push({
            name: "clinicPrescription",
          });
        }
      });
    },
    getPrescriptionDetails(id) {
      prescriptionDetails(id).then((res) => {
        if (res.code == 200) {
          this.editData = res.data.prescription;
        }
      });
    },
    submitPrescriptionEdit() {
      this.submitData = this.$refs.createComp.emitData();
      this.submitDataOption();
      let edit = {
        id: this.editId,
        template_name: this.submitData.prescription.prescription_name,
        pre_type: this.types,
        user_id: this.submitData.user_id,
        prescription: this.submitData.prescription,
      };
      prescriptionEdit(edit).then((res) => {
        if (res.code == 200) {
          console.log("修改成功");
          if(this.types==1){
            this.$router.push({
              name: "personalPrescription",
            });
          }else{
            this.$router.push({
              name: "clinicPrescription",
            });
          }
        }
      });
    },
  },
};
</script>
